package com.ecarx.module_log.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.ecarx.library_base.base_util.NotifyListener;
import com.ecarx.library_base.base_util.ThreadUtils;
import com.ecarx.module_log.R;
import com.ecarx.module_log.bean.FileBean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


public class FileOutListAdapter extends RecyclerView.Adapter<FileOutListAdapter.MyViewHolder> {

    private Context mContext;
    private NotifyListener mListener;

    private List<FileBean> mFileList = Collections.EMPTY_LIST;

    public void setFileList(List<FileBean> fileList) {
        this.mFileList = fileList;
        this.notifyDataSetChanged();
    }


    public void addFileList(List<FileBean> fileList) {
        this.mFileList.addAll(fileList);
        this.mFileList = mFileList.stream().distinct().collect(Collectors.toList());
        this.notifyDataSetChanged();
    }

    public List<FileBean> getFileList() {
        return mFileList;
    }


    public FileOutListAdapter(Context mContext, NotifyListener listener) {
        this.mContext = mContext;
        this.mListener = listener;
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_file, parent, false);
        MyViewHolder vh = new MyViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
        final FileBean bean = mFileList.get(position);
        holder.cbItem.setOnCheckedChangeListener(null);
        holder.cbItem.setChecked(bean.isChecked());
        holder.cbItem.setOnCheckedChangeListener((buttonView, isChecked) -> {
            ThreadUtils.getCachedPool().execute(() -> {
                bean.setChecked(isChecked);
                boolean checkAll = mFileList.stream().allMatch(FileBean::isChecked);
                long checkNum = getCheckCount();
                ThreadUtils.runOnUiThread(() -> mListener.onNotify(checkAll, checkNum));
            });

        });
        holder.tvFileName.setText((position + 1) + ". " + bean.getFile().getName());

    }


    @Override
    public int getItemCount() {
        return mFileList.size();
    }


    static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tvFileName;
        CheckBox cbItem;

        public MyViewHolder(View itemView) {
            super(itemView);
            cbItem = itemView.findViewById(R.id.cb_item);
            tvFileName = itemView.findViewById(R.id.tv_file_name);
        }

    }

    public void checkAll(boolean isChecked) {
        mFileList.stream().filter(fileBean -> fileBean.isChecked() != isChecked).forEach(fileBean -> fileBean.setChecked(isChecked));
        notifyDataSetChanged();
    }

    public boolean hasChecked() {
        return mFileList.stream().anyMatch(FileBean::isChecked);
    }


    public long getCheckCount() {
        return mFileList.stream().filter(FileBean::isChecked).count();
    }

}
